﻿Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.Runtime.InteropServices
Imports Microsoft.SqlServer.Server
Imports FileManipulationSQLCLRExternal.SharedObjects

Partial Public Class UserDefinedFunctions
    <Microsoft.SqlServer.Server.SqlFunction(FillRowMethodName:="MFGetFileSpecs_FillRow", IsDeterministic:=False, IsPrecise:=False, TableDefinition:="FileSize int, FileDt datetime, FileExistFlag tinyint")> _
    Shared Function MFGetFileSpecs(ByVal FilePath As SqlString, ByVal FileName As SqlString) As IEnumerable
        Dim _sFileNameWithPath(0) As String

        Try
            _sFileNameWithPath(0) = GetFileNameWPath(CType(FilePath, String), CType(FileName, String))

            Return _sFileNameWithPath
        Catch sqlex As SqlException
            Throw New Exception("FileName: " & _sFileNameWithPath(0), sqlex)
        Catch ex As Exception
            Throw New Exception("FileName: " & _sFileNameWithPath(0), ex)
        End Try
    End Function

    Shared Sub MFGetFileSpecs_FillRow(ByVal obj As Object, <Out()> ByRef FileSize As SqlInt32, <Out()> ByRef FileDt As SqlDateTime, <Out()> ByRef FileExistFlag As SqlByte)
        Dim _sFileNameWithPath As String = CType(obj, String)

        Try
            Dim _iFileSize As Integer
            Dim _dFileDt As Date
            Dim _bFileExistFlag As Boolean

            GetFileSpecs(_sFileNameWithPath, _iFileSize, _dFileDt, _bFileExistFlag)

            If _bFileExistFlag = True Then
                FileSize = CType(_iFileSize, SqlInt32)
                FileDt = CType(_dFileDt, SqlDateTime)
                FileExistFlag = CType(1, SqlByte)
            Else
                FileSize = SqlInt32.Null
                FileDt = SqlDateTime.Null
                FileExistFlag = CType(0, SqlByte)
            End If
        Catch sqlex As SqlException
            Throw New Exception("FileName: " & _sFileNameWithPath, sqlex)
        Catch ex As Exception
            Throw New Exception("FileName: " & _sFileNameWithPath, ex)
        End Try
    End Sub
End Class
